﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using LibraryO2S;
using System.Data;
public partial class Administrator_CategoryEdit : System.Web.UI.Page
{

    ConnectSQL cnts = new ConnectSQL();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            // string category_id = Request.QueryString["CATEGORY_ID"].ToString();
            BindData(category_id);
        }
    }
    string category_id
    {
        get
        {
            return Request.QueryString["CATEGORY_ID"].ToString();
        }
    }
    BizProductCategory category = new BizProductCategory();
    public void BindData(string category_id)
    {
        DataTable db = cnts.GetTableWithCommandText("select * from tblProductCategories where CATEGORY_ID=" + category_id);
        txtCategoryName.Text = db.Rows[0]["CATEGORY_NAME"].ToString();
        string status = db.Rows[0]["STATUS"].ToString();
        txtSortDisplay.Text = db.Rows[0]["DISPLAY_ORDER"].ToString();
        imgDescription.ImageUrl = "~/Categoyicon/" + db.Rows[0]["ICON"].ToString();
        if (status == "1")
        {
            cbStatus.Checked = true;
        }


    }
    public int GetRandom()
    {
        Random rd = new Random();
        int a = rd.Next(1, 100);
        return a;
    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        int status = 0;
        if (cbStatus.Checked)
        {
            status = 1;
        }
        else
            status = 0;
        try
        {

            string createdby = Session["USERNAME"].ToString();


            DataTable db = cnts.GetTableWithCommandText("select * from tblProductCategories where CATEGORY_ID=" + category_id);
            string filname = db.Rows[0]["ICON"].ToString();
            if (fuIcon.HasFile)
            {
                filname = GetRandom() + "" + fuIcon.FileName;
                fuIcon.SaveAs(Server.MapPath("~/Categoyicon/") + filname);
            }
            category.tblProductCategories_Update(int.Parse(category_id), txtCategoryName.Text, filname, int.Parse(txtSortDisplay.Text), status);
            divMessage.InnerHtml = O2S_Message.Success("Hệ thống", "Cập nhật thành công");
        }
        catch (Exception ex)
        {
            divMessage.InnerHtml = O2S_Message.Error("Hệ thống", "Phát hiện lỗi" + ex.Message);
        }

    }
}